<template>
    <view class="searchBarCity">
        <view class="city" @click="areaSelectFn">
            <image class="icon-lbs" src="/static/icon/icon_search_bar_lbs.png"></image>
            <text class="city-name">{{ current }}</text>
            <text class="icon-down"></text>
        </view>
        <view class="searchBar-main">
            <image class="icon-search" src="/static/icon/icon_search_bar_search.png"></image>
            <input
                class="input input-search"
                :placeholder="placeholderText"
                confirm-type="search"
                @confirm="searchFn"
                v-model="value"
                placeholder-class="placeholderSearchClass" />
            <text class="btn-search" @click="searchFn">搜索</text>
        </view>
        <uni-popup ref="areaSelect" type="bottom" background-color="#fff" :safe-area="false">
            <area-select-submit :address-data="location" @pickArea="pickArea" :isShowArea="false"></area-select-submit>
        </uni-popup>
    </view>
</template>

<script>
import areaSelectSubmit from "@/uni-sub-base/components/area-select/area-select-submit.vue"
export default {
    components: {
        areaSelectSubmit,
    },
    props: {
        placeholderText: {
            type: String,
            default: "",
        },
        currentCity: {
            type: String,
            default: "",
        },
        param: {
            type: Object,
            default: () => {},
        },
        location: {
            type: Object,
            default: null,
        },
    },
    watch: {
        currentCity(e) {
            this.current = e ? e : "定位中"
            this.$forceUpdate()
        },
        location(e) {
            this.$forceUpdate()
        },
    },
    data() {
        return {
            value: "",
            lbs: null,
            current: "",
        }
    },
    mounted() {
        this.current = this.currentCity ? this.currentCity : "定位中"
        this.$forceUpdate()
    },
    methods: {
        searchFn() {
            //搜索
            let key = this.value
            if (key && key.length) {
                key = key.trim()
            } else {
                key = ""
            }
            this.$emit("searchFn", key)
        },
        //选择地区
        areaSelectFn() {
            this.$refs.areaSelect.open()
        },
        reSet(location) {
            this.location = location
        },
        pickArea(data) {
            this.param.cityId = data.CityId
            this.param.address = data.all ? data.all : data.CityName ? data.CityName : data.ProvinceName
            this.$refs.areaSelect.close()
            this.location = Object.assign(this.location, data)
            this.$emit("upFn", this.param)
        },
    },
}
</script>

<style lang="scss">
.searchBarCity {
    display: flex;
    flex-direction: row;
    padding: 0 $app-item-padding;
    align-items: center;
    background: #f3f4f6;
    height: 80rpx;
    border-radius: 100rpx;
    .searchBar-main {
        display: flex;
        flex-direction: row;
        flex: 1;

        height: 72rpx;
        border-radius: 36rpx;
        align-items: center;
        padding: 0 20rpx;
        .input-search {
            display: flex;
            flex: 1;

            padding: 10rpx;
            font-size: 24rpx;
        }
        .btn-search {
            display: flex;
            height: 72rpx;
            padding-left: 20rpx;
            align-items: center;
            justify-content: center;
        }
    }
    .searchBar-actions {
        display: flex;
        flex-direction: row;
        margin-right: -20rpx;
        .btn-action-item {
            width: 88rpx;
            display: flex;
            align-items: center;
            justify-content: center;
        }
    }
}
.city {
    display: flex;
    flex-direction: row;
    align-items: center;
    border-right: solid 1rpx #dfe0e0;
    padding-right: 16rpx;
    .city-name {
        width: 100rpx;
        margin: 0 6rpx;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        font-size: $font-size-28;
        text-align: center;
    }
}
.icon-lbs {
    width: 28rpx;
    height: 28rpx;
}
.icon-search {
    width: 32rpx;
    height: 32rpx;
}
</style>
